<?php
class ImageUtility {
	
	/**
	 * Path where the uploaded images will be stored.
	 * @var unknown_type
	 */
	public $filePath = 'files\\uploaded\\images\\';
	
	public $allowExt = array('jpg', 'jpeg', 'gif');
	
	public $fieldName = 'image';
	
	/**
	 * Class Constructor
	 * Sets the ImageMagick version if applicable and the DOC ROOT.
	 */
	public function __construct($fieldName = '') {
		$this->filePath  = WWW_ROOT . $this->filePath;
		$this->fieldName = ($fieldName != '') ? $fieldName : $this->fieldName;
	}
	
	/**
	 * Uploads the file and calls resize() on success.
	 * 
	 * @param $file_name
	 * @param $resize_data
	 * @return $errors
	 */
	function uploadFile ($file, $resize_data = array()) {
		$return['error'] = array();
		$return['ket_qua'] = false;
		$fileTmpName = $file['tmp_name'];
		$fileExt = $this->fileExt($file['name']);		
		$fileName = $this->filePath . $file['name'];
		//debug($fileTmpName); exit();
		if (!is_array($file)) $return['error'][$this->fieldName] = "Vui lòng chọn một hình ảnh.";
		if (!in_array($fileExt,$this->allowExt)) $return['error'][$this->fieldName] = "File upload không hợp lệ.";
		if (file_exists($fileName)) $return['error'][$this->fieldName] = "File upload đã tồn tại.";		
		if (empty($return['error'])) {
			if (move_uploaded_file($fileTmpName, $fileName)) { 
				$return['ket_qua'] = true;
			}
		}		
		return $return;
			
	}
	
	/**
	 * Uploads the file and calls resize() on success.
	 * 
	 * @param $file_name
	 * @param $resize_data
	 * @return $errors
	 */
	function reUploadFile ($file, $oldFile, $resize_data = array()) {
		$return['error'] = array();
		$return['ket_qua'] = false;
		$fileTmpName = $file['tmp_name'];
		$fileExt = $this->fileExt($file['name']);		
		$fileName = $this->filePath . $file['name'];
		if (!is_array($file)) $return['error'][$this->fieldName] = "Vui lòng chọn một hình ảnh.";
		if (!in_array($fileExt,$this->allowExt)) $return['error'][$this->fieldName] = "File upload không hợp lệ.";
		if ($file['name'] != $oldFile && file_exists($fileName)) $return['error'][$this->fieldName] = "File upload đã tồn tại.";
		if (empty($return['error'])) {
			if ($this->removeFile($this->filePath, $oldFile)) {
				if (move_uploaded_file($fileTmpName, $fileName)) { 
					$return['ket_qua'] = true;
				}
			}			
		}		
		return $return;
			
	}

	/**
	 * Removes a file from the server.
	 *
	 * @param string $path
	 * @param string $fileName
	 * @return 
	 */
	function removeFile($path, $fileName) {
		$file = $path . $fileName;
		if (file_exists($file)) { 
			return unlink($file); 
		}
		return false;
	}
	
	/**
	 * Checks to make the File was uploaded via HTTP POST.
	 * @param $params
	 * @return unknown_type
	 */
	function isUploadedFile($params) {
		if ((isset($params['error']) && $params['error'] == 0) || 
			(!empty($params['tmp_name']) && $params['tmp_name'] != 'none')) {
			return is_uploaded_file($params['tmp_name']);
		} else {
			return false;
		}
	}
	
	/**
	 * Grabs the file extension.
	 *
	 * @param $file
	 * @return string $ext
	 */
	function fileExt ($file) {
		$ext = substr(strrchr($file, '.'), 1);
		return strtolower($ext);
	}
} 
?>
